package org.oschina.api.blog.recommend.dao;

import java.util.List;

import org.oschina.Constants;
import org.oschina.api.BaseDAO;
import org.oschina.api.blog.recommend.model.BlogRecommend;
import org.oschina.api.blog.recommend.vo.BlogRecommendVo;
import org.springframework.stereotype.Repository;

@Repository
public class BlogRecommendDAO extends BaseDAO {
	
	private final String findVo[]={"id","bid","create_time"};

	/**
	 * 返回
	 * @param first
	 * @param max
	 * @return
	 */
	public List<BlogRecommendVo> findVo(int first,int max)
	{
		return getQueryHelper().query("SELECT br.id,br.bid,br.create_time,bl.title FROM "+Constants.DB_BLOG_RECOMMEND+" br,"+Constants.DB_BLOG+" bl WHERE br.bid=bl.id ORDER BY br.create_time DESC LIMIT ?,?", 
				new Object[]{first,max}, BlogRecommendVo.class);
	}
	
	/**
	 * 返回数量
	 * @return
	 */
	public int getCount()
	{
		return (int) getQueryHelper().queryDataSum(null,null,BlogRecommendVo.class);
	}
	
	/**
	 * 根据ID返回
	 * @param id
	 * @return
	 */
	public BlogRecommendVo findVoById(int id)
	{
		return (BlogRecommendVo) getQueryHelper().queryForObject(findVo, new String[]{"id=?"}, new Object[]{id}, BlogRecommendVo.class);
	}
	
	/**
	 * 删除
	 * @param id
	 * @return
	 */
	public int delete(int id)
	{
		return (int) getQueryHelper().delete(new String[]{"id=?"}, new Object[]{id}, BlogRecommendVo.class);
	}
	
	/**
	 * 创建
	 * @param blogRecommend
	 * @return
	 */
	public BlogRecommend create(BlogRecommend blogRecommend)
	{
		getQueryHelper().insert(new String[]{"id","bid","create_time"}, new Object[]{blogRecommend.getId(),blogRecommend.getBid(),blogRecommend.getCreate_time()}, BlogRecommendVo.class);
		return blogRecommend;
	}
	
}